package com.iflytek.autofly.setting.biz.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window;
import android.widget.Toast;

/**
 * @author yutian2@iflytek.com
 */
public abstract class BaseFragmentActivity extends FragmentActivity {
    /**
     * @Fields progressDialog : 加载提示框
     */
    protected ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性。参数是Window类中定义的常量。表示无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    // //////////////////////showtoast封装start////////////////////////////
    protected void showShortToast(int pResId) {
        showShortToast(getString(pResId));
    }

    protected void showLongToast(int pResId) {
        showLongToast(getString(pResId));
    }

    protected void showLongToast(String pMsg) {
        Toast.makeText(this, pMsg, Toast.LENGTH_LONG).show();
    }

    protected void showShortToast(String pMsg) {
        Toast.makeText(this, pMsg, Toast.LENGTH_SHORT).show();
    }

    // //////////////////////showtoast封装start////////////////////////////

    // //////////////////////启动activity封装start////////////////////////////
    protected void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    protected void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    protected void openActivity(String pAction) {
        openActivity(pAction, null);
    }

    protected void openActivity(String pAction, Bundle pBundle) {
        Intent intent = new Intent(pAction);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    // //////////////////////启动activity封装end////////////////////////////

    // //////////////////////从intent获取参数封装start////////////////////////////
    // 功能包括1.查看key值对应的参数是否存在 2.可设置参数不存在时的默认值
    protected boolean hasExtra(String pExtraKey) {
        if (getIntent() != null) {
            return getIntent().hasExtra(pExtraKey);
        }
        return false;
    }

    /**
     * @param key
     * @param defaultValue
     * @return String
     * @Description: 如果key存在返回参数, 否则返回传递的默认值, 否则返回"no value"
     * @author 忧郁的西索 ★^_^+
     */
    protected String getString(String key, String... defaultValue) {
        if (hasExtra(key)) {
            return getIntent().getStringExtra(key);
        } else if (defaultValue.length != 0) {
            return defaultValue[0];
        }
        return "no value";
    }

    // //////////////////////从intent获取参数封装end////////////////////////////

    // //////////////////////加载提示框start////////////////////////////
    // 提示框属于UI进程,在界面UI发生变化(切换或退出)需要在生命周期方法中dismiss
    // 该父类在 onPause onStop 中处理
    public void showProgressDialog(CharSequence message) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setIndeterminate(true);
        }
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
    }

    /**
     * @param
     * @Description: 提示框属于UI进程, 在界面UI发生变化(切换或退出)需要在生命周期方法中dismiss，在 onPause
     * onStop 中处理
     * @author 忧郁的西索 ★^_^+
     */
    public void dismissProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    // //////////////////////加载提示框start////////////////////////////

    @Override
    protected void onResume() {
        super.onResume();
        if (getAactivityName() != null) {
            notifySystem(getAactivityName());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        dismissProgressDialog();
        if (getAactivityName() != null) {
            notifySystem("");
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        dismissProgressDialog();
    }

    protected abstract String getAactivityName();

    private void notifySystem(String name) {
        Intent intnet = new Intent("com.iflytek.car.USER_ACTION");
        intnet.putExtra("name", name);
        sendBroadcast(intnet);
    }
}
